/**
 * 公共方法
 */
import Vue from "vue";

const appCommonH = {};

/**
 * 是否是空对象
 * @param  {[type]}  data [description]
 * @return {Boolean}      [description]
 */
appCommonH.isEmptyObj = function (data) {
  if (!data) return true;

  return !Object.keys(data).length;
}

/**
 * 获取get参数
 * @param  {string} name
 * @return {string|null}
 */
appCommonH.query = function (name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
  var r = window.location.search.substr(1).match(reg);

  if (r != null) return decodeURIComponent(r[2]); return null;
}

/**
 * 获取url中全部参数
 * @param  {[type]} url [description]
 * @return {[type]}     [description]
 */
appCommonH.getUrlParam = function (url) {
  var match = url.split('?')[1].split('#')[0];
  if (!match) return {};
  var matches = match.split('&');
  var obj = {};
  for (var i = 0; i < matches.length; i++) {
    var key = matches[i].split('=')[0];
    var value = matches[i].split('=')[1];
    obj[key] = value;
  }
  return obj;
}

/**
 * 去掉链接中的多个参数
 */
appCommonH.reomveUrlParams = function (indexnames, dourl) {
  if( !indexnames || $.isEmptyObject(indexnames)) return (dourl ? dourl : window.location.href);
  if(typeof indexnames == "string") indexnames = [indexnames];

  var nowurl = dourl ? dourl : window.location.href;
  var _this = this;

  $.each(indexnames, function(ik, iv) {
    nowurl = _this.removeUrlParam(iv, nowurl);
  })

  return nowurl;
}

/**
 * 去掉单个参数
 */
appCommonH.removeUrlParam = function(indexname, dourl) {
  if( !indexname ) return (dourl ? dourl : window.location.href);

  var nowurl = dourl ? dourl.split("?") : window.location.href.split("?");
  var paramsinfo = nowurl[1] ? nowurl[1].split("&") : [];
  var newurl = nowurl[0];
  var isfirstp = true;

  $.each(paramsinfo, function(indexk, indexv) {
    indexv = indexv.split("=");

    if(indexv[0] != indexname) {
      newurl += (isfirstp ? '?'+indexv[0]+'='+indexv[1] : '&'+indexv[0]+'='+indexv[1]);
      isfirstp = false;
    }
  })

  return newurl;
}

/**
 * 当前客户端类型
 */
appCommonH.clientType = function () {
  var u = navigator.userAgent;
  var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
  var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  var isPhone = isAndroid || isiOS
  u = u.toLowerCase()
  var isWeixin = u.match(/MicroMessenger/i) == 'micromessenger' || u.match(/_SQ_/i) == '_sq_'

  return {
    isWeixin, isPhone
  }
}

/**
 *  请求地址生成
 */
appCommonH.reqUri = function(path) {
  if(process.env.NODE_ENV == 'development') {
    return '/api'+path;
  } else {
    return appConfig.onLineApiDomain+path;
  }
}

/**
 *  websocket地址获取 根据不同环境
 */
appCommonH.getWsDomain = function() {
  if(process.env.NODE_ENV == 'development') {
    return appConfig.localWebsocket;
  } else {
    return appConfig.onLineWebsocket;
  }
}

/**
 * 生成随即房间 ID
 *
 * @param  {int} min
 * @param  {int} max
 * @return {string}
 */
appCommonH.generateRoomID = function (min = 100, max = 999) {
    const randomNumber = min + Math.round(Math.random() * (max - min));
    return 'SERVER_' + (new Date).getTime() + '_' + randomNumber;
}

if (!Vue.prototype.commonHelper) {
  Vue.prototype.commonHelper = appCommonH;
}

export default appCommonH;
